﻿using System;
using System.Collections.Generic;
using System.Web.UI;
using DevExpress.Web.ASPxHeadline;
using DevExpress.Web.ASPxClasses;
using DevExpress.Web.ASPxCallback;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Interface;

public partial class MRTGMonitoring : ICPRelationshipPage
{
    
    #region Properties
    
    #endregion Properties

    #region Event Handlers

    protected override void Page_Load(object sender, EventArgs e)
    {
        m_lsPermittedRoles.Add(ICPUserRole.CAR);
        base.Page_Load(sender, e);

        Page.Title = "ICP Lintasarta: Relationship, Monitoring MRTG";
        if (!IsPostBack && !IsCallback)
        {
            
        }
    }
    protected void cbkMRTG_Callback(object sender, CallbackEventArgs e)
    {
        System.Threading.Thread.Sleep(KGlobal.AJAX_WAIT_TIME);
        if(e.Parameter.ToUpper().StartsWith("GENERATE"))
        {
            e.Result = "GENERATE" + KGlobal.DELIMITER + HandleGenerate(e.Parameter);
        }
        else if (e.Parameter.ToUpper().StartsWith("UPLOAD"))
        {
            e.Result = "UPLOAD" + KGlobal.DELIMITER + HandleUpload(e.Parameter);
        }
    }
    #endregion Event Handlers

    #region Custom Methods
    protected override void DoSearch(String sxSearchQuery)
    {
        throw new NotImplementedException();
    }
    protected String HandleGenerate(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);
        String sxReturn = String.Empty;
        String sxStartDate = String.Empty;
        String sxEndDate = String.Empty;
        String sxTimeRange = sxSplitted[4];
        if(sxTimeRange.Equals("DAILY"))
        {
            sxStartDate = DateTime.Today.AddDays(-1).ToString();
            sxEndDate = DateTime.Today.ToString();
        }
        else if (sxTimeRange.Equals("MONTHLY"))
        {
            sxStartDate = DateTime.Today.AddMonths(-1).ToString();
            sxEndDate = DateTime.Today.ToString();
        }
        else if (sxTimeRange.Equals("WEEKLY"))
        {
            sxStartDate = DateTime.Today.AddDays(-7).ToString();
            sxEndDate = DateTime.Today.ToString();
        }
        else
        {
            sxStartDate = sxSplitted[2];
            sxEndDate = sxSplitted[3];
        }
        sxReturn += MRTGManager.BuildImage(sxSplitted[1], sxStartDate, sxEndDate);
        return sxReturn;
    }
    protected String HandleUpload(String sxParameter)
    {
        String[] sxSplitted = sxParameter.Split(new String[] { KGlobal.DELIMITER }, StringSplitOptions.None);

        String sxReturn = String.Empty;
        MRTGData mrtgData = new MRTGData();
        NewsFeedData nfData = new NewsFeedData();

        String sxTimeType = String.Empty;
        String sxStartDate = String.Empty;
        String sxEndDate = String.Empty;
        String sxURL = String.Empty;

        foreach (String sxRow in sxSplitted)
        {
            String[] sxRowSplitted = sxRow.Split(new String[] { KGlobal.NEWLINE }, StringSplitOptions.None);
            switch (sxRowSplitted[0])
            {
                case KMRTG.NETWORK: 
                    mrtgData.sxNetworkNo = sxRowSplitted[1];
                    break;
                case KMRTG.TIME:
                    sxTimeType = sxRowSplitted[1];
                    break;
                case KMRTG.STARTDATE:
                    sxStartDate = sxRowSplitted[1];
                    break;
                case KMRTG.ENDDATE:
                    sxEndDate = sxRowSplitted[1];
                    break;
                case KMRTG.NOTE:
                    mrtgData.sxNote = sxRowSplitted[1];
                    break;
                case KMRTG.IMAGE:
                    sxURL = sxRowSplitted[1];
                    break;
                case KMRTG.CUSTOMERID:
                    mrtgData.iCustomerID = Convert.ToInt32(sxRowSplitted[1]);
                    break;
            }
        }
        if(sxTimeType.Equals("DAILY"))
        {
            mrtgData.dtmStartDate = DateTime.Today.AddDays(-1);
            mrtgData.dtmEndDate = DateTime.Today;
        }
        else if (sxTimeType.Equals("MONTHLY"))
        {
            mrtgData.dtmStartDate = DateTime.Today.AddMonths(-1);
            mrtgData.dtmEndDate = DateTime.Today;
        }
        else if (sxTimeType.Equals("WEEKLY"))
        {
            mrtgData.dtmStartDate = DateTime.Today.AddDays(-7);
            mrtgData.dtmEndDate = DateTime.Today;
        }
        else
        {
            mrtgData.dtmStartDate = Convert.ToDateTime(sxStartDate);
            mrtgData.dtmEndDate = Convert.ToDateTime(sxEndDate);
        }
        String sxFileName = "chart_" + mrtgData.sxNetworkNo + "_" +
                         mrtgData.dtmStartDate.ToString("yyyyMMdd") + "_" +
                         mrtgData.dtmEndDate.ToString("yyyyMMdd") + ".png";
        int iFileUploadId = FileHandler.DownloadFileByURL(sxURL,
            KFileHandler.MRTG.MRTGFILE, sxFileName);
        mrtgData.iFileUploadID = iFileUploadId;
        DBQuery.MonitoringMRTGTable.UploadMRTG(mrtgData);
        
        //save activity into newsfeed
        //String sxCustomerName = DBQuery.CustomerTable.GetCompanyNameByID(mrtgData.iCustomerID);
        nfData.iCustomerId = mrtgData.iCustomerID;
        nfData.iSourceId = (int)NewsFeedSourceType.ICP_MRTG;
        nfData.sxContent = String.Format(
            "Network No     : {0} <br/>"+
            "Start Date     : {1} <br/>"+
            "End Date       : {2} <br/>"+
            "Note           : {3}",
            mrtgData.sxNetworkNo,
            mrtgData.dtmStartDate.ToString("dd-MMM-yyyy"),
            mrtgData.dtmEndDate.ToString("dd-MMM-yyyy"),
            mrtgData.sxNote);
        nfData.sxTitle = "MRTG has been uploaded";
        nfData.sxUserId_Poster = m_sxUID;
        nfData.sxUserName_Poster = m_sxUserName;
        nfData.Type = NewsFeedDataType.FileUpload;
        DBQuery.NewsFeedTable.SaveNewsFeed(nfData);
        DBQuery.NewsFeedTable.SaveNewsFeedData((int)NewsFeedDataType.FileUpload, nfData.ID, iFileUploadId.ToString());

        
        sxReturn = MRTGManager.BuildEditor_MRTG();
        return sxReturn;
    }
    #endregion Custom Methods
}
